//
//  UIView+Toast.m
//  MobileCRM
//
//  Created by winter on 16/3/8.
//  Copyright © 2016年 江苏金恒信息科技有限公司. All rights reserved.
//

#import "UIView+Toast.h"
#import "NSString+Util.h"
static const NSInteger MarginBottom=100;

@implementation UIView(Toast)

+ (void)wsShowToastForMessage:(NSString *)message
{
    //UIView *toast = [UIView new];
    UILabel *label = [UILabel new];
    [label setFont:[UIFont systemFontOfSize:12]];
    [label setBackgroundColor:[@"#000000" wsStringConvertToUIColorWithAlpha:.85]];
    [label setTextColor:[UIColor whiteColor]];
    [label setText:message];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setNumberOfLines:0];
    //[label setTranslatesAutoresizingMaskIntoConstraints:NO];
    CGSize limit = [UIScreen mainScreen].bounds.size;
    limit.height = limit.height/2;
    limit.width = limit.height*.75;
    CGRect finalSize = [message boundingRectWithSize:limit options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil];
    finalSize.size.height += 15;
    finalSize.size.width += 20;
    [label setFrame:finalSize];
    label.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height - MarginBottom - finalSize.size.height/2);
    label.layer.cornerRadius = 5;
    label.clipsToBounds  = YES;
    label.alpha = 0;
    [[UIApplication sharedApplication].keyWindow addSubview:label];
    [UIView animateWithDuration:3 animations:^{
        label.alpha = 1;
    } completion:^(BOOL finished) {
       [UIView animateWithDuration:1 animations:^{
           label.alpha = 0;
       } completion:^(BOOL finished) {
           [label removeFromSuperview];
       }];
    }];
}

+ (void)wsShowMaskView
{
    static UIView *maskView;
    if(!maskView)
    {
        maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [maskView setBackgroundColor:[@"#000000" wsStringConvertToUIColorWithAlpha:.45]];
        [[UIApplication sharedApplication].keyWindow addSubview:maskView];
        
        UIActivityIndicatorView *indication = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        [indication setTintColor:[UIColor blackColor]];
        indication.center = maskView.center;
        indication.transform = CGAffineTransformMakeScale(2, 2);
        [maskView addSubview:indication];
        [maskView setHidden:YES];
        [indication startAnimating];
    }
    [maskView setHidden:!maskView.isHidden];
}
@end
